//
//  tryViewController.m
//  EasyToTesco
//
//  Created by ym on 16/3/28.
//  Copyright © 2016年 Jack. All rights reserved.
//

#import "tryViewController.h"
#import "HeaderFileForEasyToTesco.h"

#import "MyOrderWholeTableViewCell.h"
#import "FOrderTableViewCell.h"

//选择控制器视图
#import "SelectBtnView.h"
#import "AFNetworking.h"
#import "UIImageView+WebCache.h"
#import "ReadLoginData.h"

#import "oilTableViewCell.h"

//orderDetailVC
#import "OrderDetailViewController.h"
#import "recJiFenTableViewCell.h"
#import "selectView.h"
#define WEITH [UIScreen mainScreen].bounds.size.width
#define HIGHT [UIScreen mainScreen].bounds.size.height

#define WEITHRATE [UIScreen mainScreen].bounds.size.width/320
#define HIGHTRATE [UIScreen mainScreen].bounds.size.height/568


@interface tryViewController ()<UITableViewDataSource, UITableViewDelegate, SelectBtnViewDelegate,SelectViewDelegate,UIAlertViewDelegate>
{
    NSInteger orderstate;
    BOOL BtnState;
    NSString *usernameStr;
}

@property (nonatomic, strong)NSMutableArray *WholeArray;
@property (nonatomic, strong)NSMutableArray *fOrderArray;
@property (nonatomic, strong)NSMutableArray *OilorderArray;
@property (nonatomic, strong)NSMutableArray *dataArr;
@property (nonatomic, strong)UITableView *MyOrderTableview;
@property (nonatomic, copy)NSString *userName;

@end

@implementation tryViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.  [self getdata];
    self.view.backgroundColor = [UIColor whiteColor];
    [self getdata];
    [self addMyOrderVcinit];
    [self addMyOrderTopView];
    [self addMyOrderUI];
    [self addMyOrdertableview];
    
   
    
    
    self.MyOrderTableview.rowHeight = 70;
}


- (void)getdata {
    // 获取当前账号
    NSDictionary *dic = [ReadLoginData readLoginData];
    if (dic.allKeys.count == 0) {
        UIAlertView *alview = [[UIAlertView alloc] initWithTitle:@"提示" message:@"您尚未登录,请登录后再次操作" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
        [alview show];
    }else {
        usernameStr = [dic[@"data"] objectForKey:@"username"];
        [self MyOrderGethfordDataSources];
        [self MyOrderGethoilDataSources];
        [self MyOrderDataSources];
    }
    
}

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonInde{
   [self dismissViewControllerAnimated:YES completion:nil];
}
- (void)addMyOrderVcinit {
    orderstate = 1000;
    BtnState = YES;
    //   usernameStr = @"13282008515";
    self.WholeArray = [@[]mutableCopy];
    self.fOrderArray = [@[]mutableCopy];
    self.OilorderArray = [@[]mutableCopy];
    
}

- (void)addMyOrderTopView {
    
    UIImageView *image1 = [[UIImageView alloc] initWithFrame:CGRectMake(10, (self.secondView.frame.size.height-20)/2, 20, 20)];
    image1.image = [UIImage imageNamed:@"goback-icon.png"];
    [self.secondView addSubview:image1];
    self.leftBtn.hidden = YES;
    
    UIButton *goBackBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    goBackBtn.frame = CGRectMake(0, 0, self.secondView.frame.size.height, self.secondView.frame.size.height);
    [goBackBtn addTarget:self action:@selector(gobackClick) forControlEvents:UIControlEventTouchUpInside];
    [self.secondView addSubview:goBackBtn];

}

- (void)gobackClick {
    [self dismissViewControllerAnimated:YES completion:nil];
}

- (void)addMyOrdertableview {
    self.MyOrderTableview = [[UITableView alloc] initWithFrame:CGRectMake(0, HIGHT*0.16, WEITH, HIGHT*0.84) style:UITableViewStyleGrouped];
    self.MyOrderTableview.dataSource = self;
    self.MyOrderTableview.delegate = self;
    self.MyOrderTableview.tableFooterView = [[UIView alloc] init];
    self.MyOrderTableview.rowHeight = 80;
    [self.view addSubview:self.MyOrderTableview];
}

- (void)addMyOrderUI {
    
    selectView *selectViewA =[[selectView alloc] initWithFrame:CGRectMake(0, HIGHT*0.1, WEITH, HIGHT*0.06)];
    
    selectViewA.delegate = self;
    
    [self.view addSubview:selectViewA];
    
}

#pragma mark -- SelectBtnViewDelegate
-(void)selectBtnClick:(UIButton *)button {
    //    NSLog(@"%@----%ld", button.titleLabel.text, button.tag);
    
    orderstate = button.tag;
    
    if (button.tag == 1000) {
        BtnState = YES;
        [self MyOrderDataSources];
    } else if (button.tag == 1001) {
        BtnState = NO;
        [self MyOrderGethfordDataSources];
        
    } else {
        BtnState = NO;
        [self MyOrderGethoilDataSources];
    }
    
    
}

#pragma mark -- UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
//    return 1;
    if (orderstate == 1000) {
        return self.WholeArray.count;
    } else if (orderstate == 1001){
        return self.OilorderArray.count;
    } else if (orderstate == 1002){
        return self.fOrderArray.count;
    }else {
        return self.WholeArray.count;
    }
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}




- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *identifier = @"recJiFenTableViewCell";
    recJiFenTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if (cell == nil) {
        cell = [[[NSBundle mainBundle]loadNibNamed:@"recJiFenTableViewCell" owner:nil options:nil] lastObject];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        
    }
    
  
    
    if (orderstate == 1000) {
        NSLog(@"1000");
       cell.titleID.text = self.WholeArray[indexPath.row][@"title"];
          cell.VIPnum.text = [NSString stringWithFormat:@"会员号 : %@", [self.WholeArray[indexPath.row] objectForKey:@"username"]];
        cell.money.text = self.WholeArray[indexPath.row][@"jine"];
        if ([self.WholeArray[indexPath.row][@"zt"] isEqualToString:@"1"]) {
         cell.onOff.text = @" 状态 ：  成功";
        }else{
              cell.onOff.text = @" 状态 ：  失败";
        }

    } else if (orderstate == 1001){
         NSLog(@"1001");
        NSLog(@"1002");
        cell.titleID.text = self.OilorderArray[indexPath.row][@"title"];
        cell.money.text = self.OilorderArray[indexPath.row][@"jine"];
        
        if ([self.OilorderArray[indexPath.row][@"zt"] isEqualToString:@"1"]) {
            cell.onOff.text = @" 状态 ：  成功";
        }else{
            cell.onOff.text = @" 状态 ：  失败";
        }
        cell.VIPnum.text = [NSString stringWithFormat:@"会员号 : %@", [self.OilorderArray[indexPath.row] objectForKey:@"username"]];
        
       
    } else if (orderstate == 1002){
       
        cell.titleID.text = self.fOrderArray[indexPath.row][@"title"];
        cell.VIPnum.text = [NSString stringWithFormat:@"会员号 : %@", [self.fOrderArray[indexPath.row] objectForKey:@"username"]];
        cell.money.text = self.fOrderArray[indexPath.row][@"jine"];
        if ([self.fOrderArray[indexPath.row][@"zt"] isEqualToString:@"1"]) {
            cell.onOff.text = @" 状态 ：  成功";
        }else{
            cell.onOff.text = @" 状态 ：  失败";
        }
        
        cell.onOff.text =[NSString stringWithFormat:@"状态 ： %@",  self.fOrderArray[indexPath.row][@"ems_stant"]];
        cell.money.text =[NSString stringWithFormat:@"收货人：%@",  self.fOrderArray[indexPath.row][@"ems_name"]];
        
    }else {
        NSLog(@"else");
    }
    
    
    
    
    
    
    
    
    return cell;
}

//
//- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
//    return 150;
//}


- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 1;
}


- (void)MyOrderDataSources {
    
    
    AFHTTPSessionManager *mgr = [AFHTTPSessionManager manager];
    mgr.responseSerializer = [AFHTTPResponseSerializer serializer];
    
    NSString *username = usernameStr;

    NSDictionary *param = @{@"username":username,
                            @"usertype":@1,
                            @"fkfs":@3,
                            @"p":@1};
    
    [mgr POST:@"http://www.ylgw8.com/AppHz/Gethz" parameters:param success:^(NSURLSessionDataTask *task, id responseObject) {
//        NSString *string = [[NSString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding];
        
        self.WholeArray = [self toArrayOrNSDictionary:responseObject];
        NSLog(@"%@", self.WholeArray);
   //     NSDictionary *dic = [NSDictionary dictionaryWithDictionary:self.WholeArray[0]];
        [self.MyOrderTableview reloadData];
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        
    }];
    
    
    
}

- (void)MyOrderGethfordDataSources {
    
    AFHTTPSessionManager *mgr = [AFHTTPSessionManager manager];
    mgr.responseSerializer = [AFHTTPResponseSerializer serializer];
    
    // NSString *username = @"18620614920";//usernameStr;
    NSString *username = usernameStr;//usernameStr;
    NSDictionary *param = @{@"username":username,
                            @"usertype":@1,
                            @"fkfs":@4,
                            @"p":@1};
    

    
 
    
    [mgr POST:@"http://www.ylgw8.com/AppHz/Gethz" parameters:param success:^(NSURLSessionDataTask *task, id responseObject) {
        NSString *string = [[NSString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding];
        
        self.fOrderArray = [self toArrayOrNSDictionary:responseObject];
        NSLog(@"%@", self.fOrderArray);
  //      NSDictionary *dic = [NSDictionary dictionaryWithDictionary:self.WholeArray[0]];
        [self.MyOrderTableview reloadData];
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        
    }];
  
    
}

//oilorder
- (void)MyOrderGethoilDataSources {
    
    
    AFHTTPSessionManager *mgr = [AFHTTPRequestOperationManager manager];
    mgr.responseSerializer = [AFHTTPResponseSerializer serializer];
    
    NSString *username = usernameStr;
    NSDictionary *param = @{@"username":username,
                            @"usertype":@4,
                            @"fkfs":@2,
                            @"p":@1};

    
    [mgr POST:@"http://www.ylgw8.com/AppHz/Gethz" parameters:param success:^(NSURLSessionDataTask *task, id responseObject) {
        NSString *string = [[NSString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding];
        
        self.OilorderArray = [self toArrayOrNSDictionary:responseObject];
        
    //    NSDictionary *dic = [NSDictionary dictionaryWithDictionary:self.OilorderArray[0]];
        [self.MyOrderTableview reloadData];
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        
    }];
    
    
    
    
    
    
}


// 将JSON串转化为字典或者数组
- (id)toArrayOrNSDictionary:(NSData *)jsonData{
    NSError *error = nil;
    id jsonObject = [NSJSONSerialization JSONObjectWithData:jsonData
                                                    options:NSJSONReadingAllowFragments
                                                      error:&error];
    
    if (jsonObject != nil && error == nil){
        return jsonObject;
    }else{
        // 解析错误
        return nil;
    }
}






















- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
